package net.dao.impl;

import java.util.List;

import net.dao.BackgroundDao;
import net.form.processing.HSVRangeBackground;
import net.model.Background;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "backgroundDao")
public class BackgroundDaoImpl implements BackgroundDao {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Background getBackground() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Background.class);
		return (Background) criteria.list().get(0);
	}

	@Override
	public void setHSVRangeBackground(HSVRangeBackground rangeBackground) {
		Background background = getBackground();
		background.setHSVRangeBackground(rangeBackground);
		sessionFactory.getCurrentSession().saveOrUpdate(background);

	}

	@Override
	public Background findConfiguracion(String nombreSistema) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Background.class);
		List<Background> list = (List<Background>) criteria.add(
				Restrictions.eq("systemName", nombreSistema)).list();
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void setSeparationOfObjects(boolean separationOfObjects) {
		Background gridReference = getBackground();
		gridReference.setSeparationOfObjects(separationOfObjects);
		sessionFactory.getCurrentSession().saveOrUpdate(gridReference);
	}

}
